﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using QuanLyHocPhi.BLL;
using QuanLyHocPhi.Model;

namespace QuanLyHocPhi.GUI.DuLieu
{
    public partial class FormHocKy : FormBase
    {
        public FormHocKy()
        {
            InitializeComponent();

            gridMucThu.ReadOnly = true;
            gridMucThu.AllowUserToAddRows = false;
            gridMucThu.AllowUserToDeleteRows = false;
            gridMucThu.AutoGenerateColumns = false;

            m_NeedLoadHocKy = false;
            m_NeedLoadMucThu = false;
            NapDanhSachNamHoc();
            NapDanhSachHocKy();
            NapDanhSachMucThu();
            m_NeedLoadHocKy = true;
            m_NeedLoadMucThu = true;
            UpdateGuiBySelectedNamHoc();
            UpdateGuiBySelectedHocKy();
            UpdateGuiBySelectedMucThu();
        }

        #region SelectedNamHoc
        private NamHoc SelectedNamHoc
        {
            get
            {
                if (bsNamHoc.Current == null)
                    return null;
                else
                    return bsNamHoc.Current as NamHoc;
            }
        }
        #endregion

        #region SelectedHocKy
        private HocKy SelectedHocKy
        {
            get
            {
                if (bsHocKy.DataSource == null || bsHocKy.Current == null)
                    return null;
                else
                    return (bsHocKy.Current as HocKy);
            }
        }

        private string SelectedMaHocKy
        {
            get
            {
                var hocky = this.SelectedHocKy;
                if (hocky == null)
                    return null;
                else
                    return hocky.MaHocKy;
            }
        }
        #endregion

        private void OnLoad(object sender, EventArgs e)
        {
            m_NeedLoadHocKy = false;
            m_NeedLoadMucThu = false;
            NapDanhSachNamHoc();
            UpdateGuiBySelectedNamHoc();
            NapDanhSachHocKy();
            UpdateGuiBySelectedHocKy();
            m_NeedLoadHocKy = true;
            m_NeedLoadMucThu = true;
            NapDanhSachMucThu();
            UpdateGuiBySelectedMucThu();
        }

        private bool m_NeedLoadHocKy = true;
        private void NapDanhSachNamHoc(string tenNamHocToFocus = null)
        {
            //m_NeedLoadHocKy = false;
            cbxNamHoc.DataSource = null;
            var lstNamHoc = BizNamHoc.GetListNamHoc();
            bsNamHoc.DataSource = lstNamHoc;

            if (tenNamHocToFocus != null)
            {
                for (int i = 0; i < lstNamHoc.Count; i++)
                    if (lstNamHoc[i].TenNamHoc == tenNamHocToFocus)
                    {
                        bsNamHoc.Position = i;
                        break;
                    }
            }
            cbxNamHoc.DataSource = bsNamHoc;
            //m_NeedLoadHocKy = true;
            //NapDanhSachHocKy(this.SelectedMaHocKy);
            //UpdateGuiBySelectedHocKy();
        }

        private bool m_NeedLoadMucThu = true;
        private void NapDanhSachHocKy(string maHocKyToFocus = null)
        {
            //m_NeedLoadMucThu = false;

            cbxHocKy.DataSource = null;
            var namHoc = this.SelectedNamHoc;
            if (namHoc == null)
                bsHocKy.DataSource = null;
            else
            {
                var lstHocKy = BizHocKy.GetList_HocKy(namHoc.TenNamHoc);
                bsHocKy.DataSource = lstHocKy;
                for (int i = 0; i < lstHocKy.Count; i++)
                    if (lstHocKy[i].MaHocKy == maHocKyToFocus)
                    {
                        bsHocKy.Position = i;
                        break;
                    }
            }
            cbxHocKy.DisplayMember = "TenHocKy";
            cbxHocKy.DataSource = bsHocKy;

            //m_NeedLoadMucThu = true;
            //NapDanhSachMucThu();
            //UpdateGuiBySelectedMucThu();
        }

        private void NapDanhSachMucThu()
        {
            gridMucThu.DataSource = null;

            var hocKy = this.SelectedHocKy;
            if (hocKy == null)
            {
                bsMucThu.DataSource = null;
            }
            else
            {
                var lstMucThu = BizPhiMacDinh.GetListMucThu(hocKy.MaHocKy);
                bsMucThu.DataSource = lstMucThu;
            }

            colMucThu_HocPhi.DataPropertyName = "HocPhi";
            colMucThu_LePhi.DataPropertyName = "LePhi";
            colMucThu_MaLoaiHinh.DataPropertyName = "MaLoaiHinhDaoTao";
            colMucThu_MaLopGoc.DataPropertyName = "MaLopGoc";
            colMucThu_NamTS.DataPropertyName = "NamTuyenSinh";
            colMucThu_TenLoaiHinh.DataPropertyName = "TenLoaiHinhDaoTao";
            colMucThu_TenLopGoc.DataPropertyName = "TenLopGoc";
            gridMucThu.ReadOnly = true;
            gridMucThu.AutoGenerateColumns = false;
            gridMucThu.DataSource = bsMucThu;
        }

        private void OnGridHocKyCellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        #region OnSelectedNamHocChanged
        private void bsNamHoc_CurrentChanged(object sender, EventArgs e)
        {
            UpdateGuiBySelectedNamHoc();
            if (m_NeedLoadHocKy)
                NapDanhSachHocKy(this.SelectedMaHocKy);
        }

        private void UpdateGuiBySelectedNamHoc()
        {
            var namHoc = this.SelectedNamHoc;
            if (namHoc == null)
            {
                //cbxHocKy.Enabled = false;
            }
        }
        #endregion

        #region OnSelectedHocKyChanged
        private void bsHocKy_CurrentChanged(object sender, EventArgs e)
        {
            UpdateGuiBySelectedHocKy();
            if (m_NeedLoadMucThu)
                NapDanhSachMucThu();
        }

        private void UpdateGuiBySelectedHocKy()
        {
            var hocky = this.SelectedHocKy;
            if (hocky == null)
            {
                lnkXoaHocKy.Enabled = false;
                pnlMucThuCuaHocKy.Enabled = false;
            }
            else
            {
                lnkXoaHocKy.Enabled = true;
                pnlMucThuCuaHocKy.Enabled = true;
            }
        }
        #endregion

        private void bsMucThu_CurrentChanged(object sender, EventArgs e)
        {
            UpdateGuiBySelectedMucThu();
        }

        private void UpdateGuiBySelectedMucThu()
        {
            MucThu mucThu = null;
            if (bsMucThu.Current != null)
                mucThu = bsMucThu.Current as MucThu;

            if (mucThu == null)
            {
                btnMucThu_Sua.Enabled = btnMucThu_Xoa.Enabled = false;
            }
            else
            {
                btnMucThu_Sua.Enabled = btnMucThu_Xoa.Enabled = true;
            }
        }

        private void btnMucThu_BoSung_Click(object sender, EventArgs e)
        {
            var selectedNamHoc = this.SelectedNamHoc;
            var selectedHocKy = this.SelectedHocKy;

            var frm = new FormDangKyPhiMacDinh(selectedNamHoc, selectedHocKy);
            frm.Size = this.Size;
            frm.Location = this.Location;
            frm.StartPosition = FormStartPosition.Manual;
            if (frm.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
            {
                NapDanhSachMucThu();
            }
        }

        private void lnkTaoMoiHocKy_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            var frm = new FormTaoMoiHocKy();
            if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                var hocKy = frm.InsertedHocKy;
                if (hocKy == null)
                    return;
                m_NeedLoadHocKy = false;
                m_NeedLoadMucThu = false;
                NapDanhSachNamHoc(hocKy.NamHoc.TenNamHoc);
                UpdateGuiBySelectedNamHoc();
                NapDanhSachHocKy(hocKy.MaHocKy);
                UpdateGuiBySelectedHocKy();
                m_NeedLoadHocKy = true;
                m_NeedLoadMucThu = true;
                NapDanhSachMucThu();
                UpdateGuiBySelectedMucThu();
            }
        }

        private void btnMucThu_Xoa_Click(object sender, EventArgs e)
        {
            MucThu mucThu = null;
            if (bsMucThu.Current != null)
                mucThu = bsMucThu.Current as MucThu;
            if (mucThu == null)
            {
                DialogUtils.ShowError("Phải chọn lớp trong danh sách trước khi thực hiện xoá");
                return;
            }
            try
            {
                BizPhiMacDinh.DeleteMucThu(mucThu);

                NapDanhSachMucThu();
            }
            catch (Exception exc)
            {
                DialogUtils.ShowError("Không xoá quy định mức thu cho lớp đang chọn được.\n" +
                    "Nguyên nhân lỗi: " + exc.Message);
            }
        }

        private void btnMucThu_Sua_Click(object sender, EventArgs e)
        {
            MucThu mucThu = null;
            if (bsMucThu.Current != null)
                mucThu = bsMucThu.Current as MucThu;
            if (mucThu == null)
            {
                DialogUtils.ShowError("Phải chọn lớp trong danh sách trước khi thực hiện điều chỉnh");
                return;
            }
            var tenLop = string.Format("Điều chỉnh đối với lớp {0}", mucThu.TenLopGoc);
            var hocKy = string.Format("học kỳ {0} năm học {1}", this.SelectedHocKy.TenHocKy, this.SelectedNamHoc.ToString());
            var frm = new FormDieuChinhMucThu(mucThu, tenLop, hocKy);
            if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                bsMucThu.ResetCurrentItem();
            }
        }

        private void btnMucThu_SaoChep_Click(object sender, EventArgs e)
        {
            var namHocToFocus = this.SelectedNamHoc;
            var frm = new FormSelectHocKy(namHocToFocus, maHocKyToFocus: null, excludedMaHocKy: this.SelectedMaHocKy);
            if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                var hocKy = frm.SelectedHocKy;
                try
                {
                    BizPhiMacDinh.SaoChepMucThu(
                        fromMaHocKy: hocKy.MaHocKy, 
                        toMaHocKy: this.SelectedMaHocKy);
                    NapDanhSachMucThu();
                }
                catch (Exception exc)
                {
                    DialogUtils.ShowError("Lỗi trong quá trình sao chép mức thu\nChi tiết lỗi " + exc.Message);
                }
            }
        }

        private void lnkXoaHocKy_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            var hocKy = this.SelectedHocKy;
            if (hocKy == null)
            {
                DialogUtils.ShowError("Phải chọn học kỳ để xoá");
                return;
            }
            var namHoc = this.SelectedNamHoc;
            var ans = DialogUtils.ShowYesNo("Bạn có chắc chắn muốn xoá thông tin học kỳ {0} năm học {1} không?\n\n" +
                "Lưu ý: Chỉ được xoá học kỳ sau khi các quy định mức thu của học kỳ đó đã được xoá hết.",
                hocKy.TenHocKy, namHoc.ToString());
            if (ans != System.Windows.Forms.DialogResult.Yes)
                return;
            try
            {
                BizHocKy.DeleteHocKy(hocKy);

                // Nạp lại dữ liệu
                m_NeedLoadHocKy = false;
                m_NeedLoadMucThu = false;
                NapDanhSachNamHoc(hocKy.NamHoc.TenNamHoc);
                UpdateGuiBySelectedNamHoc();
                NapDanhSachHocKy();
                UpdateGuiBySelectedHocKy();
                m_NeedLoadHocKy = true;
                m_NeedLoadMucThu = true;
                NapDanhSachMucThu();
                UpdateGuiBySelectedMucThu();
            }
            catch (Exception exc)
            {
                DialogUtils.ShowError(exc.Message);
            }
        }

        private void gridMucThu_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            btnMucThu_Sua.PerformClick();
        }
    }
}
